package com.zline.foodraise.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.photoselector.listener.OnPhotoItemCheckedListener;
import com.photoselector.listener.PhotoItemListener;
import com.photoselector.model.PhotoModel;
import com.zline.foodraise.listener.RecyclerItemClicked;

import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * Created by heshantao on 15/8/23.
 * 新的PhotoItem(重新整合)
 */
@EViewGroup(resName = "new_photo_item")
public class NewPhotoItem extends RelativeLayout {
    private static final String LOG_TAG = NewPhotoItem.class.getSimpleName();
    @ViewById(resName = "iv_new_photoitem")
    ImageView photo_iv;
    @ViewById(resName = "cb_new_photoitem")
    CheckBox photo_cb;
    int position;
    PhotoModel photoModel;
    RecyclerItemClicked itemClicked;
    OnPhotoItemCheckedListener checkedListener;
    PhotoItemListener photoItemListener;
    public NewPhotoItem(Context context) {
        super(context);
    }

    public void bind(PhotoModel photoModel, int position,DisplayImageOptions options) {
        this.position = position;
        this.photoModel=photoModel;
        ImageLoader.getInstance().displayImage(
                "file://" + photoModel.getOriginalPath(), photo_iv, options);
        photo_cb.setChecked(photoModel.isChecked());
    }

    //checkbox 选择事件
    @CheckedChange(resName = "cb_new_photoitem")
    void CheckChange(CompoundButton buttonView, boolean isChecked) {
        Log.i(LOG_TAG, "________是否点击_______" + isChecked);
        boolean isReachMax = photoItemListener.reachMax();
        checkedListener.onCheckedChanged(photoModel, buttonView, isChecked); // 调用主界面回调函数
        // 让图片变暗或者变亮
        if (isChecked) {
            if (!isReachMax) {
                setDrawingable();
                photo_iv.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                photoModel.setChecked(isChecked);
            }
        } else {
            photo_iv.clearColorFilter();
            photoModel.setChecked(isChecked);
        }

    }

    @Click(resName = "iv_new_photoitem")
    void onClick(){
        itemClicked.onRecyclerItemClicked(position);
    }

    private void setDrawingable() {
        photo_iv.setDrawingCacheEnabled(true);
        photo_iv.buildDrawingCache();
    }

    public void setRecycleItemClickListener(RecyclerItemClicked itemClickListener){
        this.itemClicked=itemClickListener;
    }

    public void setPhotoItemCheckListener(OnPhotoItemCheckedListener checkListener){
        this.checkedListener=checkListener;
    }

    public void setPhotoItemListener(PhotoItemListener listener){
        this.photoItemListener=listener;
    }


}
